/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegrasFrame.java
 *
 * Created on 21/03/2010, 19:29:33
 */
package br.com.janalise.view;

import br.com.janalise.control.MapaDoTextoController;
import br.com.janalise.model.MapaDoTexto;
import br.com.janalise.model.Regra;
import br.com.janalise.system.Configurar;
import br.com.janalise.util.BuscarPadrao;
import java.util.Collections;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.TreePath;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;
import br.com.janalise.util.ProcuradorDeRegras;
import br.com.janalise.util.Repositorio;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

/**
 *
 * @author boskyn9
 */
public class RegrasFrame extends PaiFrame {

    private String[] node;
    private Image question = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/question.png"));
    private MapaDoTextoController mapaControl = new MapaDoTextoController();
    /** Creates new form RegrasFrame */
    public RegrasFrame() {
        initComponents();
        new Repositorio().getTreeModelFiles(jTree1, repositorio,null);
    }

    private void chart(List<Regra> regras, String nome, int inicio, int fim) {
        DefaultCategoryDataset data = new DefaultCategoryDataset();


        for (int i = inicio; i < fim; i++) {
            if (regras.get(i) != null) {
                data.setValue(regras.get(i).getOcorrencia(), regras.get(i).getPercentual(regras.size())+" %", regras.get(i).getChave());
            }
        }

        JFreeChart chart = ChartFactory.createStackedBarChart3D(
                "Regras Chart ("+nome+")", "regras", "Ocorrencias",
                data,
                jRadioButton1.isSelected()?PlotOrientation.HORIZONTAL:jRadioButton2.isSelected()?PlotOrientation.VERTICAL:PlotOrientation.VERTICAL,
                false, // legend?
                true, // tooltips?
                false // URLs?
                );

        ChartFrame frame = new ChartFrame("JAnalise Chart", chart);
        frame.setIconImage(this.getIconImage());
        frame.pack();
        RefineryUtilities.centerFrameOnScreen(frame);
        frame.setVisible(true);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jSliderFim = new javax.swing.JSlider();
        jPanel6 = new javax.swing.JPanel();
        jSliderInicio = new javax.swing.JSlider();
        jPanel7 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        info = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jSplitPane1.setDividerLocation(300);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jSplitPane2.setDividerLocation(100);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jXTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jXTable1.setEditable(false);
        jXTable1.setFont(new java.awt.Font("DialogInput", 0, 12));
        jScrollPane4.setViewportView(jXTable1);

        jTabbedPane1.addTab("Regras", jScrollPane4);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jTabbedPane1.addTab("Log", jScrollPane2);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/chart.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuração"));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Valor final"));

        jSliderFim.setMajorTickSpacing(20);
        jSliderFim.setMinorTickSpacing(10);
        jSliderFim.setPaintTicks(true);
        jSliderFim.setValue(10);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSliderFim, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jSliderFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Valor inicial"));

        jSliderInicio.setMajorTickSpacing(20);
        jSliderInicio.setMinorTickSpacing(10);
        jSliderInicio.setPaintTicks(true);
        jSliderInicio.setValue(0);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSliderInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jSliderInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Horizontal");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setSelected(true);
        jRadioButton2.setText("Vertical");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 300, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jScrollPane3.setViewportView(jPanel2);

        jTabbedPane1.addTab("Gráfico", jScrollPane3);

        jSplitPane2.setRightComponent(jTabbedPane1);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Gerar Regras"));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/gerar.png"))); // NOI18N
        jButton1.setMnemonic('g');
        jButton1.setText("Gerar Regras");
        jButton1.setToolTipText("Gerar Regras");
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/gerar.png"))); // NOI18N
        jButton3.setMnemonic('d');
        jButton3.setText("Definições");
        jButton3.setToolTipText("Refazer definições sobre o texto");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        info.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(info, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(4, 4, 4))
            .addComponent(info, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
        );

        jSplitPane2.setLeftComponent(jPanel3);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 826, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-862)/2, (screenSize.height-506)/2, 862, 506);
    }// </editor-fold>//GEN-END:initComponents

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        jTabbedPane1.setSelectedIndex(0);
        jTextArea1.setText("");
        info.setText("");
        TreePath[] paths = jTree1.getSelectionPaths();
        node = new String[paths.length];

        for (int i = 0; i < paths.length; i++) {
            TreePath treePath = paths[i];
            node[i] = Configurar.getNode(treePath);
        }

        if (node.length == 1) {
            MapaDoTexto mapa = mapaControl.findByNome(node[0]);
            if (mapa != null) {
                if(mapa.getDefinicoes()==null){
                    mapa.setDefinicoes(BuscarPadrao.conceitos(mapa));
                    mapaControl.save(mapa);
                    info.setText("Fim.");
                }

                jTextArea1.setText(mapa.getDefinicoes());

                List<Regra> regras = mapa.getRegras();
                Collections.sort(regras);
                jSliderInicio.setMaximum(regras.size());
                jSliderFim.setMaximum(regras.size());
                jXTable1.setModel(this.setModel(regras));

                jXTable1.getColumn(0).setPreferredWidth(30);
                jXTable1.getColumn(0).setResizable(false);
                jXTable1.getColumn(1).setPreferredWidth(100);
                jXTable1.getColumn(1).setResizable(false);
                jXTable1.getColumn(2).setPreferredWidth(90);
                jXTable1.getColumn(2).setResizable(false);
                jXTable1.getColumn(3).setPreferredWidth(500);
                jXTable1.getColumn(3).setResizable(true);
            } else {
                JOptionPane.showMessageDialog(this, "O sistema não conseguiu converter "+ node[0] +" em texto", "Aviso", JOptionPane.WARNING_MESSAGE);
            }
        } 

    }//GEN-LAST:event_jTree1ValueChanged

    private DefaultTableModel setModel(List<Regra> regras) {
        Object[] coluna = {"#", "Chave", "Ocorrencias", "Semelhantes"};
        DefaultTableModel model = new DefaultTableModel(coluna, regras.size());

        for (int i = 0; i < regras.size(); i++) {
            if (regras.get(i) != null) {
                model.setValueAt(i + 1, i, 0);
                model.setValueAt(regras.get(i).getChave(), i, 1);
                model.setValueAt(regras.get(i).getOcorrencia(), i, 2);
                model.setValueAt(regras.get(i).getSemelhantes(), i, 3);
            }
        }
        return model;

    }

//    private MapaDoTexto consulta(String node) {
//        Conexao con = Conexao.getInstance();
//        ODB odb = con.getOdb();
//        IQuery query = new CriteriaQuery(MapaDoTexto.class, Where.equal("nome", node));
//        Objects<Object> objetos = odb.getObjects(query);
//        MapaDoTexto mapa = null;
//        if (!objetos.isEmpty()) {
//            mapa = (MapaDoTexto) odb.getObjects(query).getFirst();
//        }
//        return mapa;
//    }

    @SuppressWarnings("static-access")
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (node != null) {
            jTabbedPane1.setSelectedIndex(1);
            for (int i = 0; i < node.length; i++) {
                MapaDoTexto mapa = mapaControl.findByNome(node[i]);
                if(mapa==null){
                    JOptionPane.showMessageDialog(this, "O sistema não conseguiu converter "+ node[i] +" em texto", "Aviso", JOptionPane.WARNING_MESSAGE);
                }else{
                    if (mapa.getRegras() == null || mapa.getRegras().isEmpty()) {
                        Thread finder = new ProcuradorDeRegras(mapa, jTextArea1);
                        finder.start();
                    } else {
                        Object opcao[] = {"Sim","Não"};
                        int operacao = JOptionPane.showOptionDialog(null, "O elemento " + mapa.getNome() + " já possui regas, deseja redefinir?", "Aviso", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,new ImageIcon(question),opcao,opcao[1]);
                        if (operacao == JOptionPane.YES_OPTION) {
                            Thread finder = new ProcuradorDeRegras(mapa, jTextArea1);
                            finder.start();
                        }
                    }
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um elemnto na árvore", "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (node != null) {
            if (node.length == 1) {
                MapaDoTexto mapa = mapaControl.findByNome(node[0]);
                if (mapa != null) {
                    List<Regra> regras = mapa.getRegras();
                    Collections.sort(regras);
                    chart(regras, mapa.getNome(), jSliderInicio.getValue(), jSliderFim.getValue());
                } else {
                    System.out.println("consulta nada");
                }
            } else {
                JOptionPane.showMessageDialog(this, "Selecione apenas um elemnto na árvore", "Aviso", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um elemnto na árvore", "Aviso", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (node != null) {
            if (node.length == 1) {                
                MapaDoTexto mapa = mapaControl.findByNome(node[0]);
                mapa.setDefinicoes(BuscarPadrao.conceitos(mapa));
                mapaControl.save(mapa);
                info.setText("Fim.");
            }
        } else {
            info.setText("");
            JOptionPane.showMessageDialog(this, "Selecione um elemnto na árvore", "Aviso", JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel info;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSlider jSliderFim;
    private javax.swing.JSlider jSliderInicio;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTree jTree1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
